<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">

<div class="modal fade" id="modal-create-adj">
    <div class="modal-dialog" style="width:80%">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">新增库存调整</h4>
            </div>
            <div class="modal-body">
                <form id="inveAdjCreateForm" class="form-horizontal">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">物料描述</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" name="aadAfterMatName" placeholder="请输入物料描述">
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">在库数量</label>
                                    <div class="col-sm-8">
                                        <input type="value" class="form-control" name="aadAfterNum" placeholder="请输入数量">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">单价（元）</label>
                                    <div class="col-sm-8">
                                        <input type="value" class="form-control" name="aadAfterPrice" placeholder="">
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">单位</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" name="aadAfterUnit" placeholder="请输入单位">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">图号</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" name="aadAfterDrawingNo" placeholder="">
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">品牌</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" name="aadAfterBrand" placeholder="">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">SAP物料编码</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" name="aadAfterSapMatCode" placeholder="">
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label" th:value="${aadAfterProjectCode}">项目编号</label>
                                    <div class="col-sm-8">
                                        <input class="form-control" list="aadAfterProjectCodeList"
                                               onchange="changeProName()"
                                               oninput="findProCodeListData(this)"
                                               name="aadAfterProjectCode"
                                               id="aadAfterProjectCode"
                                               autocomplete="off"

                                               type="text"
                                               placeholder="请输入需求项目编号"/>
                                        <datalist id="aadAfterProjectCodeList">
                                        </datalist>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">项目名称</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" name="aadAfterProjectName" placeholder="">
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">部位名称</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" name="aadAfterPart" placeholder="">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">区位</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" name="aadAfterPosition" placeholder="">
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">仓位</label>
                                    <div class="col-sm-8">
                                        <select name="aadAfterWhiId" class="form-control">
                                            <option value="" selected="selected">请选择仓库</option>
                                            <option th:each="whiIds:${whiIds}" th:value="${whiIds.whiId}" th:text="${whiIds.whiName}"></option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">跟踪号</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" name="aadAfterTrackCode" placeholder="">
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">物料分类</label>
                                    <div class="col-sm-8">
                                        <select name="aadAfterMatType" class="form-control">
                                            <option value="" selected="selected">请选择物料分类</option>
                                            <option value=1>外购件</option>
                                            <option value=2>外协件</option>
                                            <!--<option value=3>制作件</option>-->
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">箱号</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" name="aadAfterCaseNo" placeholder="">
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">装箱日期</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control datepicker" name="aadAfterBoxingDate" placeholder="请选择"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">在库类型</label>
                                    <div class="col-sm-8">
                                            <select name="aadAfterStockType" class="form-control">
                                                <option value="" selected="selected">请选择在库类型</option>
                                                <option value=1>库存在库</option>
                                                <option value=2>项目在库</option>
                                            </select>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">备注</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" name="aadAfterRemark" placeholder="">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">调整原因</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" name="aadAfterSpecialInstructions" placeholder="">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </form>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="btn-creat-save" shiro:hasPermission="halinv:haladjlist:edit">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
</div>

<div class="modal fade" id="modal-edit-adj">
    <div class="modal-dialog" style="width:80%">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">库存调整</h4>
            </div>
            <div class="modal-body">
                <form id="inveForm" class="form-horizontal">
                    <input type="hidden" id="ityId" name="ityId">
                    <input type="hidden" id="num" name="num">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">物料描述</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="ityMatName" id ="ityMatName" placeholder="">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">在库数量</label>
                                <div class="col-sm-8">
                                    <input type="value" class="form-control" name="ityNum" id="ityNum" placeholder="" onkeyup="changeWeight(this)">
                                </div>
                            </div>
                        </div>
                        <div id="aluminumDiv">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">材质</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" name="ityTexture" id ="ityTexture" placeholder="" readonly="readonly">
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">在库重量</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" name="ityWeight" id="ityWeight" placeholder="" readonly="readonly">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">单价（元）</label>
                                <div class="col-sm-8">
                                    <input type="value" class="form-control" name="ityPrice" placeholder="">
                                </div>
                            </div>
                        </div>
                        <div id="aluminumDiv_unit">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">单位</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" name="ityUnit" placeholder="">
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">图号</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="ityDrawingNumber" placeholder="" readonly="readonly">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">品牌</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="ityBrand" placeholder="" readonly="readonly">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">SAP物料编码</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="itySapCode" placeholder="" readonly="readonly">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">项目编号</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="ityProCode" placeholder="" readonly="readonly">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">项目名称</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="ityProName" placeholder="" readonly="readonly">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">部位名称</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="ityPart" placeholder="" readonly="readonly" >
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">区位</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="ityPosition" placeholder=""  readonly="readonly" >
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">仓位</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="ityWhiId" readonly="readonly">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">跟踪号</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="ityTrackCode" placeholder="" readonly="readonly" >
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">物料分类</label>
                            <div class="col-sm-8">
                                <input type="hidden" class="form-control" name="ityMatType">
                                <input class="form-control" id="ityMatType" type="text" disabled="disabled">
                            </div>
                        </div>
                        </div>
                        <div class="col-md-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">箱号</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="ityCaseNo" placeholder="" readonly="readonly">
                            </div>
                        </div>
                        </div>
                        <div class="col-md-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">装箱日期</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="ityBoxingDate" placeholder="" readonly="readonly">
                            </div>
                        </div>
                        </div>
                        <div class="col-md-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">在库类型</label>
                                <div class="col-sm-8">
                                    <input type="hidden" class="form-control" name="ityStockType">
                                    <input  type="text"  class="form-control" id="ityStockType"  disabled="disabled">
                                </div>
                        </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                            <label class="col-sm-4 control-label">备注</label>
                            <div class="col-sm-8">
                                <input readonly="readonly" type="text" class="form-control" name="ityRemark" placeholder="" >
                            </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                            <label class="col-sm-2 control-label">调整原因</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="ityCause" placeholder="">
                            </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="btn-inve-adj" shiro:hasPermission="halinv:haladjlist:edit">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<section class="content-header">
    <h1>库存列表</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group" style="margin-right: 10px;">
                                    <label>物料描述</label>
                                    <input type="text" name="ityMatName" class="form-control" placeholder="请输入物料描述">
                                </div>
                                <div class="form-group" style="margin-right: 10px;">
                                    <label>项目名称</label>
                                    <input type="text" name="ityProName" class="form-control" placeholder="请输入项目名称">
                                </div>
                                <div class="form-group" style="margin-right: 10px;">
                                    <label>项目编号</label>
                                    <input type="text" name="ityProCode" class="form-control" placeholder="请输入项目编号">
                                </div>
                                <button type="button" class="btn btn-success" id="btn-search">
                                    <span class="Bold">&emsp;搜索&emsp;</span>
                                </button>
                            </div>
                        </div>
                        <div id="toolbar">
                            <!--<a class="btn btn-info pull-right"  onclick="createInveAdj()" style="margin-right: 10px" shiro:hasPermission="halinv:haladjlist:edit">
                                <span class="Bold">新增库存调整</span>
                            </a>-->
                        </div>
                    </form>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table" style="word-break:break-all; word-wrap:break-all;"></table>
                </div>
            </div>
        </div>
    </div>
</section>

<div th:include="include :: footer"></div>

<script th:inline="javascript">

    function changeWeight() {
        if($("#ityMatType").val() == "铝料"){
            var a = parseFloat($("#ityNum").val());
            var b = parseFloat($("#num").val());
            var c = Math.round(a*b*1000)/1000;
            $("#ityWeight").val(c);
        }
    }
    /**
     * 打开编辑库存模态框
     */
    function openEditAdj(ityId) {
        js.post({
            url: ctx+"half/halInvManage/halfInveAdjApply/invAdj",
            data: {
                id: ityId
            },
            success:function (result) {
                $('#inveId').val(ityId);
                $("#inveForm").fillData(result);
                $('#ityIfFinishedProduct').val(result.ityIfFinishedProduct);
                $('#ityMatType').val(result.ityMatType);
                $('#ityStockType').val(result.ityStockType);
                if($("#ityMatType").val() == 2){
                    $("#aluminumDiv").hide();
                    $("#aluminumDiv_unit").show();
                }else if($("#ityMatType").val() == 4){
                    $("#aluminumDiv").show();
                    $("#aluminumDiv_unit").hide();
                    $("#num").val(result.ityWeight/result.ityNum) ;
                }
                if($("#ityMatType").val() == 1){
                    $("#ityMatType").val("外购件");
                }else if($("#ityMatType").val() == 2){
                    $("#ityMatType").val("外协件");
                }else if($("#ityMatType").val() == 3){
                    $("#ityMatType").val("制作件");
                }else if($("#ityMatType").val() == 4){
                    $("#ityMatType").val("铝料");
                }
                if($("#ityStockType").val() == 1){
                    $("#ityStockType").val("库存在库");
                }else if($("#ityStockType").val() == 2){
                    $("#ityStockType").val("项目在库");
                }else $("#ityStockType").val("");

                js.modal.open("modal-edit-adj");
                js.modal.closeLoading();
            }
        })
    }

    /**
     *@FunctionName: findProCodeListData
     *@Description: 根据项目号模糊查询获取项目号列表
     */
    function findProCodeListData(obj) {
        $.post(ctx + 'purchase/change/findProCodeList', {
            code: $(obj).val()
        }, function (result) {
            if (result.type == 'SUCCESS') {
                $("#aadAfterProjectCodeList").empty();
                for (var i = 0; i < result.data.length; i++) {
                    $("#aadAfterProjectCodeList").append('<option value="' + result.data[i].code + '" data="' + result.data[i].name + '" data-status="' + result.data[i].status + '"></option>');
                }
            }
        }, 'json');
    }

    function changeProName() {
        var input_select = $("#inveAdjCreateForm [name='aadAfterProjectCode']").val();
        var option_length = $("#aadAfterProjectCodeList").find("option").length;
        for (var i = 0; i < option_length; i++) {
            if (input_select == $("#aadAfterProjectCodeList option").eq(i).attr('value')) {
                $("#inveAdjCreateForm [name='aadAfterProjectName']").val($("#aadAfterProjectCodeList option").eq(i).attr("data"));
            }
        }
    }

    /**
     * 打开新增库存调整模态框
     */
    function createInveAdj() {
         js.modal.open("modal-create-adj");
    }

    var table, backType;
    $(function () {
        /**
         * 加载列表
         */
        table = js.table.init({
            url: ctx + "half/halInvManage/halfInveAdjApply/invList",
            onLoadSuccess: function (){
                $('#btn-search').prop('disabled', false).removeClass('disabled');
            },
            columns: [
                {
                    title: '序号', field: 'id', width: '40',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {title: '物料描述', field: 'ityMatName', width: '400'},
                {title: '项目名称', field: 'ityProName', width: '300'},
                {title: '项目编号', field: 'ityProCode', width: '150'},
                {title: '图号', field: 'ityDrawingNumber', width: '150'},
                {title: '品牌', field: 'ityBrand',width: '150'},
                {title: 'SAP物料编码', field: 'itySapCode',width: '150'},
                {title: '跟踪号', field: 'ityTrackCode',width: '150'},
                {title: '物料分类', field: 'ityMatType',width: '150',
                    formatter: function (value, row, index) {
                        if (value == 1) {
                            return "外购件";
                        }else if(value == 2){
                            return "外协件";
                        }else if(value == 3){
                            return "制作件";
                        }else if(value == 4){
                            return "铝料";
                        }else {
                            return "" ;
                        }
                    }
                },
                {title: '在库类型', field: 'ityStockType',width: '150',
                    formatter: function (value, row, index) {
                        if (value == 1) {
                            return "库存在库";
                        }else if(value == 2){
                            return "项目在库";
                        }else return "";
                    }
                },
                {title: '数量', field: 'ityNum', width: '80'},
                {title: '单位', field: 'ityUnit', width: '80'},
                {title: '单价</br>（元）', field: 'ityPrice', width: '80'},
                {title: '总金额</br>（元）', field: 'ityAmount', width: '120'},
                {
                    title: '操作', width: '100',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (hasP("halinv:haladjlist:edit")) {
                            actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="openEditAdj(' + row.ityId + ')"> 调整</a> ');
                        }
                        return actions.join('');
                    }
                }
            ]
        });

        /**
         * 保存变更库存
         */
        $("#btn-inve-adj").click(function (){
            js.submit({
                url: ctx + "half/halInvManage/halfInveAdjApply/save",
                data:
                     new FormData($("#inveForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                        js.modal.hide("modal-edit-adj");
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                }
            });
        });

        /**
         * 保存新增库存调整
         */
        $("#btn-creat-save").click(function () {
            js.validSubmit({
                formId: "inveAdjCreateForm",
                url: ctx + "half/halInvManage/halfInveAdjApply/createSave",
                data:
                    new FormData($("#inveAdjCreateForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                        js.modal.hide("modal-create-adj");
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                }
            });
        });

        /** 验证 */
        js.validate.init("inveAdjCreateForm", {
            fields: {
                aadAfterNum: {validators:
                                    {notEmpty: {message: '请输入在库数量'},
                                     digits: {message: '请输入数值'}}},
                aadAfterPrice:{validators: {digits: {message: '请输入数值'}}},
                aadAfterMatName: {validators: {notEmpty: {message: '未输入物料描述'}}},
                aadAfterStockType: {validators: {notEmpty: {message: '未选择在库类型'}}},
                aadAfterMatType: {validators: {notEmpty: {message: '未选择物料类型'}}},
                aadAfterUnit: {validators: {notEmpty: {message: '未输入单位'}}},
                aadAfterWhiId: {validators: {notEmpty: {message: '未选择仓库'}}},
                aadAfterProjectCode: {
                    validators: {
                        notEmpty: {message: '请输入需求项目编号'},
                        threshold: 6, //有6字符以上才发送ajax请求，（input中输入一个字符，插件会向服务器发送一次，设置限制，6字符以上才开始）
                        remote: {//ajax验证。server result:{"valid",true or false} 向服务发送当前input name值，获得一个json数据。例表示正确：{"valid",true}
                            type: 'POST',//请求方式
                            url: ctx + "half/halInvManage/halfInveAdjApply/codeExist",//验证地址
                            message: '项目号不存在',//提示消息
                            delay: 100
                        }
                    }
                },
            }
        });

        /**
         * 表格搜索
         */
        $("#btn-search").click(function () {
            $('#btn-search').prop('disabled', true).addClass('disabled');
            js.table.search(table);
        });

        /**
         * 模态框隐藏事件， 重置表单，重置验证
         */
        js.modal.hideEvent("modal-create-adj", function () {
            js.validate.reset("inveAdjCreateForm");
            js.reset("inveAdjCreateForm");
        })
    });
</script>
</body>
</html>